package com.jwds.digital.bios.business.service.service.dispatch.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchLargeScaleEquipmentReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchLargeScaleEquipmentAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchLargeScaleEquipment;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchLargeScaleEquipmentMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchLargeScaleEquipmentRespVO;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchLargeScaleEquipmentService;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 实现类
 * @author 林柏云
 * @date 2024-12-23
 */
@Slf4j
@Service
public class DispatchLargeScaleEquipmentServiceImpl extends ServiceImpl<DispatchLargeScaleEquipmentMapper, DispatchLargeScaleEquipment> implements IDispatchLargeScaleEquipmentService {

  @Autowired
  private DispatchLargeScaleEquipmentMapper baseMapper;

  @Override
  public DispatchLargeScaleEquipmentRespVO queryById(Long id) {
    if(id == null) {
      return null;
    }
    DispatchLargeScaleEquipment entity = baseMapper.selectById(id);
    return DispatchLargeScaleEquipmentAssembler.entity2dto(entity);
  }

  @Override
  public PageResponse<DispatchLargeScaleEquipmentRespVO> page(PageRequest<DispatchLargeScaleEquipmentReqDTO> request) {
    DispatchLargeScaleEquipmentReqDTO condition = request.getQueryCondition();
    Page<DispatchLargeScaleEquipmentRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
    IPage<DispatchLargeScaleEquipmentRespVO> pageData = baseMapper.pageQuery(page, condition);
    return new PageResponse<>((Page<?>)pageData, pageData.getRecords());
  }


  @Override
  public void add(DispatchLargeScaleEquipmentReqDTO reqDto) {
    DispatchLargeScaleEquipment entity = DispatchLargeScaleEquipmentAssembler.dto2entity(reqDto);
    baseMapper.insert(entity);
  }

  @Override
  public void update(DispatchLargeScaleEquipmentReqDTO reqDto) {
    DispatchLargeScaleEquipment oldEntity = baseMapper.selectById(reqDto.getId());
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    DispatchLargeScaleEquipment newEntity = DispatchLargeScaleEquipmentAssembler.dto2entity(reqDto, oldEntity);
    baseMapper.updateById(newEntity);
  }

  @Override
  public void delete(Long id) {
    DispatchLargeScaleEquipment oldEntity = baseMapper.selectById(id);
    if (oldEntity == null) {
      throw new RecordNotFoundException();
    }
    baseMapper.deleteById(id);
  }


}
